package com.atguigu.async;

import com.atguigu.exception.SleepUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

public class FutureDemo01 {
    public static void main(String[] args) throws Exception {
        runAsync();
//        supplyAsync();
        System.out.println("main线程在执行");
    }

    //发起一个异步请求
    /**
     * main线程在执行
     * 你好runAsync
     */
    public static void runAsync() {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("你好runAsync");
                SleepUtils.sleep(2);
                System.out.println("另外线程在等待");
            }
        });
    }

    //发起一个异步请求
    /**
     * 你好supplyAsync 睡2秒
     * 0722
     * main线程在执行
     */
    public static void supplyAsync() throws Exception {
        CompletableFuture<String> supplyFuture = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                System.out.println("你好supplyAsync");
                SleepUtils.sleep(2);
                return "0722";
            }
        });
        //得到返回值的方法 是一个阻塞方法
        System.out.println(supplyFuture.get());
    }

}
